package com.aqie.demo.Face;

import com.aqie.demo.HIKNetSDK.HCNetSDK;
import com.aqie.demo.utils.PropertyUtils;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;

import java.io.IOException;
import java.util.Scanner;

/**
 * @author jiangxin
 * @create 2021-04-13-9:51
 */
@Slf4j
public class FaceMain {

    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;

    public static void main(String[] args) throws InterruptedException, IOException, DocumentException {
        FaceMain faceMain=new FaceMain();
        FaceLibManage faceLibManage=new FaceLibManage();
        FacePicManage facePicManage=new FacePicManage();
        FDSearch fdSearch=new FDSearch();
        FCSearch fcSearch=new FCSearch();
        Alarm alarm = Alarm.getAlarmInstance();

        /**初始化*/
        hCNetSDK.NET_DVR_Init();

        /**加载日志*/
        boolean i= hCNetSDK.NET_DVR_SetLogToFile(3, "..//sdklog", false);

        /**登录*/
        int userID1 = faceMain.Login(PropertyUtils.getProperties("ip1"), PropertyUtils.getProperties("name"), PropertyUtils.getProperties("password"));
        alarm.SetAlarm(userID1);
        int userID2 = faceMain.Login(PropertyUtils.getProperties("ip2"), PropertyUtils.getProperties("name"), PropertyUtils.getProperties("password"));
        alarm.SetAlarm(userID2);
        int userID3 = faceMain.Login(PropertyUtils.getProperties("ip3"), PropertyUtils.getProperties("name"), PropertyUtils.getProperties("password"));
        alarm.SetAlarm(userID3);
        int userID4 = faceMain.Login(PropertyUtils.getProperties("ip4"), PropertyUtils.getProperties("name"), PropertyUtils.getProperties("password"));
        alarm.SetAlarm(userID4);
        int userID5 = faceMain.Login(PropertyUtils.getProperties("ip5"), PropertyUtils.getProperties("name"), PropertyUtils.getProperties("password"));
        alarm.SetAlarm(userID5);
        int userID6 = faceMain.Login(PropertyUtils.getProperties("ip6"), PropertyUtils.getProperties("name"), PropertyUtils.getProperties("password"));
        alarm.SetAlarm(userID6);
        // 这样会放回两个userid 两个值是不一样的
        /**获取所有人脸库信息:ID*/
//        faceLibManage.getAllFaceLib(lUserID);
        /**获取指定人脸库的信息*/
//        faceLibManage.getOneFaceLib(lUserID,"6C5791A3708B4C418388E05FCD6F6BD9");

        /**创建一个人脸库*/
//        faceLibManage.creatFaceLib(lUserID);

        /**删除一个人脸库*/
//        faceLibManage.deleteFaceLib(lUserID);

        /**上传人脸图片到人脸库*/
//        facePicManage.uploadPic(lUserID,"2CC0120275C04A5393BB5EED9FAE75E1");
        /**查询指定人脸库的人脸信息*/
//        facePicManage.searchFaceLibData(lUserID);

        /**获取人脸比对库图片数据附加信息*/
/*                FDID=A5F41447CB5E41E296C92369CF1873F5
                PID=3E0D576B7675466FBF500D2B6A362809*/
//        facePicManage.getFaceLibPicInfo(lUserID,"A5F41447CB5E41E296C92369CF1873F5","3E0D576B7675466FBF500D2B6A362809");

        /**删除人脸比对库图片数据(包含附加信息)*/
//        facePicManage.deleteFaceLibPicData(lUserID,"A5F41447CB5E41E296C92369CF1873F5","3E0D576B7675466FBF500D2B6A362809");

        /**以图搜图*/
//        fdSearch.analysisImage(lUserID);
//        fdSearch.faceLibSearch(lUserID);

        /**查询设备中存储的人脸比对结果信息*/
//        fcSearch.faceMatchListSearch(lUserID);

        /**报警布防实时接收人脸比对的结果信息*/
        log.info("用户句柄 {} {} {} {} {} {}", userID1, userID2, userID3, userID4, userID5, userID6);



        while (true) {
            //这里加入控制台输入控制，是为了保持连接状态，当输入Y表示布防结束
            log.info("请选择是否撤出布防(Y/N)：");
            Scanner input = new Scanner(System.in);
            String str = input.next();
            if (str.equals("Y")) {
                break;
            }
        }
        // 退出
        faceMain.Logout(alarm, userID1);
        faceMain.Logout(alarm, userID2);
        faceMain.Logout(alarm, userID3);
        faceMain.Logout(alarm, userID4);
        faceMain.Logout(alarm, userID5);
        faceMain.Logout(alarm, userID6);

    }


    /**
     * 登录
     */
    public int Login(String ip , String admin , String pwd) {
        //注册
        int lUserID;
        HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();//设备登录信息
        String m_sDeviceIP = ip;//设备ip地址
        m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
        System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
        String m_sUsername = admin;//设备用户名
        m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
        System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length());
        String m_sPassword = pwd;//设备密码
        m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
        System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length());
        m_strLoginInfo.wPort = 8000;
        m_strLoginInfo.bUseAsynLogin = false; //是否异步登录：0- 否，1- 是
        m_strLoginInfo.write();
        HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();//设备信息
        lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
        if (lUserID == -1) {
            log.info("登录失败，错误码为" + hCNetSDK.NET_DVR_GetLastError());
            return -1;
        } else {
            log.info("登录成功！");
            return lUserID;
        }
    }


    /**
     * 登出操作
     * @param alarm 传入报警监听句柄
     */
    public void Logout(Alarm alarm, int lUserID){

        /**退出之前判断布防监听状态，并做撤防和停止监听操作*/
        if (Alarm.map.get(lUserID) >= 0){
            if (!hCNetSDK.NET_DVR_CloseAlarmChan_V30(Alarm.map.get(lUserID))){
                log.info("撤防失败，错误码：" + hCNetSDK.NET_DVR_GetLastError());
            }else{
                log.info("撤防成功！！！");
            }
        }
        if (alarm.lListenHandle >= 0){
            if (!hCNetSDK.NET_DVR_StopListen_V30(alarm.lListenHandle)){
                log.info("取消监听失败，错误码：" + hCNetSDK.NET_DVR_GetLastError());
            }else{
                log.info("停止监听成功！！！");
            }
        }

        /**登出和清理，释放SDK资源*/
        if (lUserID>=0)
        {
            hCNetSDK.NET_DVR_Logout(lUserID);
        }
        hCNetSDK.NET_DVR_Cleanup();
    }
}
